/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2021. All rights reserved.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ohos.openharmony_qsvideoplayer.slice;

import com.ohos.openharmony_qsvideoplayer.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.window.service.WindowManager;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import org.song.videoplayer.*;
import org.song.videoplayer.media.BaseMedia;
import org.song.videoplayer.media.HarmonyMedia;

/**
 * 原实现文件应该是按照ListVideoSlice.java文件实现，不过缺少关键接口listContainer.setOnScrollListener，public void onScrollStateChanged(AbsListView view, int scrollState)
 * 以及public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount。
 * 因此为了展示功能，暂时用此界面代替。
 * 等到后续接口实现，可以去掉ListVideoSlice.java的注释代码，重新实现功能。
 */

public class ViewListSlice extends AbilitySlice implements Component.ClickedListener {
    private String url;
    private Class<? extends BaseMedia> decodeMedia;
    private DemoQSVideoView myDemoQSVideoView;
    private DemoQSVideoView demoQSVideoView1, demoQSVideoView2, demoQSVideoView3, demoQSVideoView4, demoQSVideoView5, demoQSVideoView6;
    private String mp4 = "https://mos-vod-drcn.dbankcdn.cn/P_VT/video_injection/A91343E9D/v3/9AB0A7921049102362779584128/MP4Mix_H.264_1920x1080_6000_HEAAC1_PVC_NoCut.mp4";

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(com.ohos.openharmony_qsvideoplayer.ResourceTable.Layout_ability_list_video);
        WindowManager.getInstance().getTopWindow().get().setTransparent(true);

        initView();
    }

    public void initView() {
        demoQSVideoView1 = (DemoQSVideoView) findComponentById(ResourceTable.Id_view_qs1);
        demoQSVideoView2 = (DemoQSVideoView) findComponentById(ResourceTable.Id_view_qs2);
        demoQSVideoView3 = (DemoQSVideoView) findComponentById(ResourceTable.Id_view_qs3);
        demoQSVideoView4 = (DemoQSVideoView) findComponentById(ResourceTable.Id_view_qs4);
        demoQSVideoView5 = (DemoQSVideoView) findComponentById(ResourceTable.Id_view_qs5);
        demoQSVideoView6 = (DemoQSVideoView) findComponentById(ResourceTable.Id_view_qs6);

        setPlayStatus(demoQSVideoView1);
        setPlayStatus(demoQSVideoView2);
        setPlayStatus(demoQSVideoView3);
        setPlayStatus(demoQSVideoView4);
        setPlayStatus(demoQSVideoView5);
        setPlayStatus(demoQSVideoView6);

        setPlayListener(demoQSVideoView1);
        setPlayListener(demoQSVideoView2);
        setPlayListener(demoQSVideoView3);
        setPlayListener(demoQSVideoView4);
        setPlayListener(demoQSVideoView5);
        setPlayListener(demoQSVideoView6);

        play(demoQSVideoView1, mp4, HarmonyMedia.class);
        myDemoQSVideoView = demoQSVideoView1;
    }

    public void setPlayStatus(DemoQSVideoView demoQSVideoView) {
        demoQSVideoView.enterFullMode = 3;
        //是否非全屏下也可以手势调节进度
        demoQSVideoView.isWindowGesture = true;
        demoQSVideoView.setDecodeMedia(HarmonyMedia.class);
        demoQSVideoView.bindAbilitySlice(ViewListSlice.this);
        demoQSVideoView.setUIWithStateAndMode();
        demoQSVideoView.setUp(mp4, "这是一个标题");
        demoQSVideoView.openCache();
        demoQSVideoView.getCoverImageView().setImageAndDecodeBounds(ResourceTable.Media_cover);
    }

    private void play(DemoQSVideoView demoQSVideoView, String url, Class<? extends BaseMedia> decodeMedia) {
        demoQSVideoView.release();
        demoQSVideoView.setDecodeMedia(decodeMedia);

        demoQSVideoView.setUp(
                QSVideo.Build(url).title("这是标清标题").definition("标清").resolution("标清 720P").build(),
                QSVideo.Build(url).title("这是高清标题").definition("高清").resolution("高清 1080P").build(),
                QSVideo.Build(url).title("这是2K标题").definition("2K").resolution("超高清 2K").build(),
                QSVideo.Build(url).title("这是4K标题").definition("4K").resolution("极致 4K").build()

        );
        demoQSVideoView.openCache(); //缓存配置见最后,缓存框架可能会出错,
        demoQSVideoView.play();
        this.url = url;
        this.decodeMedia = decodeMedia;
    }

    public void setPlayListener(DemoQSVideoView demoQSVideoView) {
        demoQSVideoView.setPlayListener(new PlayListener() {
            int mode;

            @Override
            public void onStatus(int status) {//播放状态
                if (status == IVideoPlayer.STATE_AUTO_COMPLETE)
                    demoQSVideoView.quitWindowFullscreen();//播放完成退出全屏
            }

            @Override//全屏/普通/浮窗
            public void onMode(int mode) {
                this.mode = mode;
            }

            @Override
            public void onEvent(int what, Integer... extra) {
                if (what == org.song.videoplayer.DemoQSVideoView.EVENT_CONTROL_VIEW && mode == IVideoPlayer.MODE_WINDOW_NORMAL) {
                    Util.CLEAR_FULL(ViewListSlice.this);
                }
            }

        });
    }

    private boolean playFlag;//记录退出时播放状态 回来的时候继续播放
    private int position;//记录销毁时的进度 回来继续该进度播放
    private EventHandler handler = new EventHandler(EventRunner.getMainEventRunner());

    @Override
    public void onActive() {
        super.onActive();
        System.out.println("===onActive===>");
        if (playFlag) {
            myDemoQSVideoView.play();
        }
        handler.removeTask(runnable);
        if (position > 0) {
            myDemoQSVideoView.seekTo(position);
            position = 0;
        }

        if (myDemoQSVideoView != null) {
            if (getDisplayOrientation() == 0) {//横屏
                myDemoQSVideoView.enterWindowFullscreen();
            } else {
                myDemoQSVideoView.quitWindowFullscreen();
            }
        }

    }

    @Override
    protected void onBackground() {
        super.onBackground();
        //暂停
        playFlag = myDemoQSVideoView.isPlaying();
        myDemoQSVideoView.pause();
    }

    @Override
    protected void onStop() {
        super.onStop();
        System.out.println("===onStop===>");
        releaseView();
        handler.removeTask(runnable);
    }

    @Override
    protected void onInactive() {
        super.onInactive();
        //进入后台不马上销毁,延时15秒
        handler.postTask(runnable, 1000 * 15);
    }

    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            if (myDemoQSVideoView.getCurrentState() != IVideoPlayer.STATE_AUTO_COMPLETE)
                position = myDemoQSVideoView.getPosition();
            myDemoQSVideoView.release();
        }
    };

    public void releaseView() {
        if (demoQSVideoView1 != null) {
            demoQSVideoView1.release();
        }
        if (demoQSVideoView2 != null) {
            demoQSVideoView2.release();
        }
        if (demoQSVideoView3 != null) {
            demoQSVideoView3.release();
        }
        if (demoQSVideoView4 != null) {
            demoQSVideoView4.release();
        }
        if (demoQSVideoView5 != null) {
            demoQSVideoView5.release();
        }
        if (demoQSVideoView6 != null) {
            demoQSVideoView6.release();
        }
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
//            case ResourceTable.Id_view_qs1:
//                myDemoQSVideoView.release();
//                play(demoQSVideoView1, mp4, HarmonyMedia.class);
//                myDemoQSVideoView = demoQSVideoView1;
//                break;
//
//            case ResourceTable.Id_view_qs2:
//                myDemoQSVideoView.release();
//                play(demoQSVideoView2, mp4, HarmonyMedia.class);
//                myDemoQSVideoView = demoQSVideoView2;
//                break;
//
//            case ResourceTable.Id_view_qs3:
//                myDemoQSVideoView.release();
//                play(demoQSVideoView3, mp4, HarmonyMedia.class);
//                myDemoQSVideoView = demoQSVideoView3;
//                break;
//
//            case ResourceTable.Id_view_qs4:
//                myDemoQSVideoView.release();
//                play(demoQSVideoView4, mp4, HarmonyMedia.class);
//                myDemoQSVideoView = demoQSVideoView4;
//                break;
//
//            case ResourceTable.Id_view_qs5:
//                myDemoQSVideoView.release();
//                play(demoQSVideoView5, mp4, HarmonyMedia.class);
//                myDemoQSVideoView = demoQSVideoView5;
//                break;
//
//            case ResourceTable.Id_view_qs6:
//                myDemoQSVideoView.release();
//                play(demoQSVideoView6, mp4, HarmonyMedia.class);
//                myDemoQSVideoView = demoQSVideoView6;
//                break;
        }
    }
}
